import numpy as np

# 拉格朗日中值定理验证
def f(x):
    return x**3

def df(x):
    return 3*x**2

a, b = -1, 0
average_slope = (f(b) - f(a)) / (b - a)

# 寻找满足条件的点
x_values = np.linspace(a, b, 1000)
for x in x_values:
    if abs(df(x) - average_slope) < 0.001:
        xi = x
        break

print(f"平均斜率: {average_slope}")
print(f"在x={xi:.3f}处，导数为: {df(xi):.3f}")
print(f"验证：两者是否接近？{abs(df(xi) - average_slope) < 0.001}")